﻿using SDK.Scripts.App;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityModule;
using DG.Tweening;

public class PanelLoad : MonoBehaviour
{
    public Button m_btnKf;
    public Slider m_slider;
    public TextMeshProUGUI m_txt;
    public GameObject PanelTip;
    public Button m_btnClose;
    public Button m_btnCancel;
    public Button m_btnOk;

    public GameObject PanelTipAge;
    public Button m_btnAge;
    public Button m_btnAgeClose;
    public Button m_btnAgeConfim;


    public static PanelLoad Instance;

    private DownloadSpeedHelper m_downloadSpeedHelper = new DownloadSpeedHelper();
    private int m_count;
    private int m_fullCount;

    private long m_size = 0;
    private long m_fullSize = 0;

    public TextMeshProUGUI m_txtInfo;
    public GameObject m_scrollObj;

    private void Awake()
    {
        Instance = this;
        m_downloadSpeedHelper.Reset();
#if !UNITY_IOS
        ShowSlider(true);
#else
        ShowSlider(false);
#endif
    }

    private void Start()
    {
        m_btnKf.onClick.AddListener(() =>
        {
            SDK.Scripts.App.GameSDK.GamePlat.DoAction(126);
        });

        m_btnClose.onClick.AddListener(CancelUpdate);
        m_btnCancel.onClick.AddListener(CancelUpdate);
        m_btnOk.onClick.AddListener(GoUpdate);

        m_btnAge.onClick.AddListener(ShowTipAge);
        m_btnAgeClose.onClick.AddListener(CloseTipAge);
        m_btnAgeConfim.onClick.AddListener(CloseTipAge);
    }

    private void CancelUpdate()
    {
        PanelTip.SetActive(false);
        VersionComponent.Instance.GetCdnUrlFailed();
    }
    private void GoUpdate()
    {
        PanelTip.SetActive(false);
        VersionComponent.Instance.ReqCdnInfo();
    }


    public void ShowKF(string channel)
    {
        if(channel.ToLower().Contains("mi") || channel.ToLower().Contains("blbl.cyddz"))
        {
            m_btnKf.gameObject.SetActive(false);
        }
    }
    public void ShowTip()
    {
        PanelTip.SetActive(true);
    }
    public void ShowTxt(string str)
    {
        m_txt.text = str;
    }
    public void SetVersionInfo(string str)
    {
        m_scrollObj.SetActive(true);        
        m_txtInfo.text = str;
    }
    public void ShowSlider(bool show)
    {
        m_slider.gameObject.SetActive(show);
        m_txt.gameObject.SetActive(show);
    }
    public void SetProgress(float pro)
    {
        m_slider.value = pro;
    }

    public void ShowTipAge()
    {
        PanelTipAge.SetActive(true);

        Transform panelTrans = PanelTipAge.transform.Find("p");
 
        panelTrans.localScale = new Vector3(0.2f, 0.2f, 1);
        Sequence seq = DOTween.Sequence();
        seq.Append(panelTrans.DOScale(Vector3.one, 0.2f));
        seq.Append(panelTrans.DOScale(new Vector3(0.97f, 0.97f, 1), 0.2f));
        seq.Append(panelTrans.DOScale(Vector3.one, 0.15f));
    }

    public void CloseTipAge()
    {
        PanelTipAge.SetActive(false);
    }

    //下载进度
    public void SetDownloadProgress(int count, int fullCount, long size, long fullSize)
    {
        m_count = count;
        m_fullCount = fullCount;
        m_size = size;
        m_fullSize = fullSize;

        m_downloadSpeedHelper.SetProgress(size, fullSize);

        double sizeD = m_size;
        double fullSizeD = m_fullSize;
        float progress = (float)(sizeD / fullSizeD);
        //Debug.LogError($"progress: {progress}, {sizeD}/{fullSizeD}");

        SetProgress(progress);

        string m_speedStr = m_downloadSpeedHelper.CalculationSpeed();

        string sizeStr = DownloadSpeedHelper.Size2Str(sizeD);
        string fullSizeStr = DownloadSpeedHelper.Size2Str(fullSizeD);

        //ShowTxt(string.Format("{0:0.00}% ({1})", progress * 100, m_speedStr));
        //ShowTxt(string.Format("{0}/{1} [{2}/{3}]", sizeStr, fullSizeStr, m_count, m_fullCount));
        ShowTxt(string.Format("{0}/{1} {2}", sizeStr, fullSizeStr, m_speedStr));
    }
}
